﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using KingRoad.EPM.SafetyManagementBLL.Domain;
using KingRoad.EPM.SafetyManagementEntity.Model;
using Unis.Framework.Core.Utility;
using KingRoad.EPM.SafetyManagementDAL.ScmAuditService;
using KingRoad.EPM.SafetyManagementDAL.ScmDailycheckService;
using KingRoad.EPM.SafetyManagementDAL.PlmPersonLeaveService;


namespace KingRoad.EPM.SafetyManagementBLL.UIRules
{
    /// <summary> 
    /// 客户端业务处理类。 
    /// 文件生成时间 2017/1/12 13:59:37
    /// </summary> 
    public class ScmAuditUIRule
    {
        private readonly IScmAuditService _scmAuditService;
        private ScmDailycheckUIRule _scmDailycheckUIRule;
        private PlmPersonleaveUIRule _plmPersonleaveUIRule;

        public ScmAuditUIRule() { }

        /// <summary> 
        /// construct function... 
        /// </summary> 
        public ScmAuditUIRule(IScmAuditService scmAuditService)
        {
            _scmAuditService = scmAuditService;
        }

        /// <summary>
        /// 根据条件获取审批列表
        /// </summary>
        /// <param name="pid">外键</param>
        /// <returns></returns>
        public List<ScmAuditInfo> GetAuditListByPID(string pid)
        {
            string sql = " 1=1 ";
            if(pid!="")
                sql=" A_PrimayID='"+pid+"' ";
            return _scmAuditService.GetEntityList(sql);
        }
        /// <summary>
        /// 根据主键获取审批信息
        /// </summary>
        /// <param name="id">主键</param>
        /// <returns></returns>
        public ScmAuditInfo GetAuditInfo(Guid id)
        {
            return _scmAuditService.GetEntityById(id);
        }
        /// <summary>
        /// 添加审批信息
        /// </summary>
        /// <param name="info">审批信息</param>
        /// <param name="type">审批类型（1=日常检查审批，2=人员请假审批）</param>
        /// <param name="state">审批状态</param>
        public void AddAudit(ScmAuditInfo info,int type,int state)
        {
            //修改主表状态
            if (type == 1)
            {
                //日常检查
                _scmDailycheckUIRule = new ScmDailycheckUIRule(new ScmDailycheckService());
                var dInfo=_scmDailycheckUIRule.GetDailycheckInfo(new Guid(info.A_PrimayID));
                dInfo.Dc_State = state;
                dInfo.Dc_FeedbackInfo = info.A_Remark;
                _scmDailycheckUIRule.EditDailycheck(dInfo);
            }
            if (type == 2)
            {
                //人员请假
                _plmPersonleaveUIRule = new PlmPersonleaveUIRule(new PlmPersonleaveService());
                var Info = _plmPersonleaveUIRule.GetInfoByID(new Guid(info.A_PrimayID));
                Info.Pl_State = state;
                _plmPersonleaveUIRule.UpdateEntity(Info);
            }
            //添加审批信息
            _scmAuditService.InsertEntity(info);
        }
        /// <summary>
        /// 修改审批信息
        /// </summary>
        /// <param name="info">审批信息</param>
        public void EditAudit(ScmAuditInfo info)
        {
            _scmAuditService.UpdateEntity(info);
        }
        /// <summary>
        /// 删除审批信息
        /// </summary>
        /// <param name="id"></param>
        public void DeleteAudit(string id)
        {
            _scmAuditService.DeleteEntityByStringID(id);
        }
    }
}
